package com.feishi.core.analytic.packing.contentpacking;

import com.feishi.core.util.common.ByteUtil;
import com.feishi.core.analytic.element.AnalyticContentBodyElement;
import com.feishi.core.analytic.element.AnalyticContentValueElement;
import com.feishi.core.analytic.packing.AnalyticDataPacking;
import com.feishi.core.analytic.packing.debuild.DeBuildSession;

/**
 * Created by caixq on 5/16 016.
 */
public class AnalyticContentValuePacking extends AnalyticContentPacking implements ContentLengthPacking,ContentNumberPacking{
    private AnalyticContentValueElement contentElement;

    private byte[] length;
    private byte[] number;

    public AnalyticContentValuePacking(AnalyticContentValueElement contentElement) {
        super();
        this.contentElement=contentElement;
    }

    public AnalyticContentValueElement getContentElement() {
        return contentElement;
    }

    public void setContentElement(AnalyticContentValueElement contentElement) {
        this.contentElement = contentElement;
    }

    public byte[] getLength() {
        return length;
    }

    public void setLength(byte[] length) {
        this.length = length;
    }

    public int getContentLength() {

        int length= (int) contentElement.getContentLength().getValue(this.length);
        int number=1;
        if(contentElement.getContentNumber()!=null){
            number= (int) contentElement.getContentNumber().getValue(this.number);
        }

        return length*number;
    }

    @Override
    public void debuildContent(byte[] bytes) {

    }

    @Override
    public void debuildContent() {

    }

    public void initData() {
        if(contentElement.getContentNumber()==null){
            this.length=this.contentElement.getContentLength().getBytes(content.length);
            return;
        }
        int length=(int)contentElement.getContentLength().getValue(this.length);
        int number= (int) Math.ceil(content.length*1.0/length);
        content= ByteUtil.fixLength(content,length*number);
        this.number=contentElement.getContentNumber().getBytes(number);



    }

    public void setContent(Object obj) {
        this.content = this.contentElement.getBytes(obj);
    }

    @Override
    public byte[] getNumber() {
        return this.number;
    }

    @Override
    public void setNumber(byte[] number) {
        this.number=number;

    }

    public void setLength(int length) {
        this.length=contentElement.getContentLength().getBytes(length);
    }

    public int getLengthValue() {
        return (int) contentElement.getContentLength().getValue(this.length);
    }

    public Object getContentValue() {
        return this.contentElement.getValue(this.content);
    }
}
